package ru.arriah.servicedesk.web.leadExecutor.action;

import java.io.IOException;
import java.util.Collection;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ru.arriah.common.web.action.ActionMapping;
import ru.arriah.common.web.action.DumbAction;
import ru.arriah.servicedesk.bean.GroupBean;
import ru.arriah.servicedesk.bean.RequestBean;
import ru.arriah.servicedesk.bean.RequestPriorityBean;
import ru.arriah.servicedesk.bean.RequestTypeBean;
import ru.arriah.servicedesk.ejb.GroupManagerLocalHome;
import ru.arriah.servicedesk.ejb.GroupManagerLocalObject;
import ru.arriah.servicedesk.ejb.RequestManagerLocalHome;
import ru.arriah.servicedesk.ejb.RequestManagerLocalObject;
import ru.arriah.servicedesk.help.Utils;
import ru.arriah.servicedesk.web.commonaction.exception.InternalException;
import ru.arriah.servicedesk.web.commonaction.exception.PrivilegesException;

public class RequestEditFormAction extends DumbAction {
  protected RequestManagerLocalHome requestManagerHome;
  protected GroupManagerLocalHome groupManagerHome;  
  
  public RequestEditFormAction() throws NamingException {
    super();    
    
    requestManagerHome =
       Utils.getRequestManagerHomeInterface();
    
    groupManagerHome =
      Utils.getGroupManagerHomeInterface();            
  }
  
  
  public String execute(
    ActionMapping actionMapping,
    HttpServletRequest request,
    HttpServletResponse response
  ) throws IOException, ServletException {
    try {
       
       int userId = Utils.getUserId(request);
       
       RequestManagerLocalObject requestManager = 
          requestManagerHome.create();      
      
      GroupManagerLocalObject groupManager = 
        groupManagerHome.create();                   
      
      int requestId = idVal(request.getParameter("request_id"));
      RequestBean requestBean = requestManager.selectRequestDetails(requestId);
      
      
      if (requestBean.getLeadExecutorId() != userId)
         throw new PrivilegesException();
      
      
      if (requestBean.getRequestStatus().getId() == 6) {       
        addErrorMessage(
           request,
           config.getProperty("ERROR.EDIT_DENIED")
        );
        
        request.setAttribute("requestBean", requestBean);
        requestManager.remove();   
        return actionMapping.getFailureTarget();
      }
      
      
      Collection<GroupBean> groupsList = 
        groupManager.selectGroups();      
      
      Collection<RequestTypeBean> requestTypesList = 
        requestManager.selectRequestTypes();            
      
      Collection<RequestPriorityBean> priorityList = 
        requestManager.selectPriorityList();
      
      requestManager.remove();
      groupManager.remove();
      
      request.setAttribute("action", actionMapping.getActionName());      
      
      request.setAttribute("priorityList", priorityList);      
      request.setAttribute("requestBean", requestBean);    
      request.setAttribute("requestTypesList", requestTypesList);
      request.setAttribute("groupsList", groupsList);
      
      return actionMapping.getSuccessTarget();
    } catch (Exception e) {
      handleException(e, "execute");
      throw new InternalException("ErrorMessage.internal");
    }
  }
}